import { get, post, getNormal } from '../common/ajax';
import { USR_USER_URL, USR_BRAND_URL, USR_STORE_URL, USR_COMMON_URL, USR_ACTIVITY_URL } from '../constants/url';
import { USER_EXT_INFO_SCENE } from '../constants/user';
import { COS_CONF, DEFAULT_MINI_QRCODE } from '../constants/common'
import { getQiniuToken, setQiniuToken, getConfigInfo, setConfigInfo } from '../biztools/wex';
import { dealActivityListData, dealActivityGroupByBeginAtData } from '../biztools/activity';
import { ENV } from '../config';

/**
 * 获取省市区
 */
export const getAreaList = async () => {
    const res = await getNormal(USR_COMMON_URL.areaList);
    return res;
};

/**
 * 获取基本配置信息
 */
export const getConfig = async (isCommon = false) => {
    if (!isCommon && getConfigInfo()) return getConfigInfo();
    const res = await getNormal(USR_COMMON_URL.config);
    res && setConfigInfo(res[ENV]);
    if (isCommon) {
        return res.common || {};
    }
    return res;
};

/**
 * 获取七牛上传token
 */
export const getUploadToken = async () => {
    if (getQiniuToken()) return getQiniuToken();
    const res = await post(USR_COMMON_URL.uploadQiniuToken, {
        bucket: COS_CONF.bucket,
        expires: 86400000, // 有效期一天
    });
    const token = res.data || '';
    // 本地保存的七牛上传token信息
    token && setQiniuToken(token);
    return token;
};

/**
 * 获取Cos上传token
 */
export const getCosAuthorization = async () => {
    // cos sdk自带cache控制,这边就不做cache了
    // if (getQiniuToken()) return getQiniuToken();
    const res = await get(USR_COMMON_URL.getCosAuthorization, {
        bucketPrefix: COS_CONF.bucketPrefix,
    });
    // cos sdk自带cache控制,这边就不做cache了
    return res.data;
};

/**
 * 获取页面小程序码
 * @param {String} page 页面路径
 * @param {String} scene 参数 brandId=1&activityId=2
 */
export const getQRcode = async (params) => {
    const res = await post(USR_COMMON_URL.createQR, params, false);
    return res.data || `${COS_CONF.path}/${DEFAULT_MINI_QRCODE}`;
};

/**
 * 获取品牌配置信息
 */
export const getBrandConfig = async (brandId) => {
    const res = await get(USR_BRAND_URL.brandConfig(brandId));
    return res.data || {};
};

/**
 * 获取分享的品牌信息
 * @param {String|Number} brandId 品牌id
 */
export const getShareBrandInfo = async (brandId) => {
    const res = await get(USR_BRAND_URL.brandInfo(brandId));
    return res.data || {};
};

/**
 * 获取分享的品牌的活动
 * @param {String|Number} brandId 品牌Id
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getShareBrandActivityList = async (params) => {
    params.pageSize = params.pageSize || 100;
    const res = await get(USR_ACTIVITY_URL.brandOwnActivityList, params);
    const { total = 0, rows = [] } = res.data || {};
    return dealActivityGroupByBeginAtData(dealActivityListData(rows));
};

/**
 * 获取分享的店铺信息
 * @param {String|Number} storeId 店铺id
 */
 export const getShareStoreInfo = async (storeId) => {
    const res = await get(USR_STORE_URL.storeInfo(storeId));
    return res.data || {};
};

/**
 * 获取分享的店铺的活动
 * @param {String|Number} storeId 店铺Id
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getShareStoreActivityList = async (params) => {
    params.pageSize = params.pageSize || 100;
    const res = await get(USR_ACTIVITY_URL.storeOwnActivityList, params);
    const { total = 0, rows = [] } = res.data || {};
    return dealActivityGroupByBeginAtData(dealActivityListData(rows));
};

/**
 * 获取分享的用户信息
 * @param {Number|String} userId 用户id
 */
export const getSharelUserInfo = async (userId) => {
    const res = await get(USR_USER_URL.specialUserInfo(userId), {
        page: USER_EXT_INFO_SCENE.home,
    });
    return res.data || {};
};

/**
 * 获取快递公司信息列表
 */
export const getExpressCompanyList = async () => {
    const res = await get(USR_COMMON_URL.expressList);
    return res.data || [];
};
